/**
 * Created by sunshine on 17-4-19.
 */
$(document).ready(function () {
    //用户名输入框失去焦点
    $('#username').blur(function () {
        var userName = this.value.trim();
        if(userName !== null && userName !== ''){
            var postData = {
                userName:userName
            };
            $.ajax({
                url:'ajaxIfHadUserName',
                type:'post',
                data:postData,
                dataType:'json',
                success:function (result) {
                    if(result.status === true){
                        var usernameHelpBlock = $('#usernameHelpBlock');
                        usernameHelpBlock.html('用户名已存在');
                        usernameHelpBlock.attr('style','color: red;');
                        usernameHelpBlock.show();
                        $('#username').parent().addClass("has-error");
                    }else{
                        var usernameHelpBlock = $('#usernameHelpBlock');
                        usernameHelpBlock.html('可以注册');
                        usernameHelpBlock.attr('style','color: green;');
                        usernameHelpBlock.show();
                        $('#username').parent().removeClass('has-error');
                    }
                },
                error:function (request) {
                    alert('通讯故障');
                }
            })
        }else {
            $('#usernameHelpBlock').hide();
        }
    });
    //密码验证
    $('#passwordConfirm').keyup(function () {
        var passwordConfirmHelpBlock = $('#passwordConfirmHelpBlock');
        if($('#passwordConfirm').val() === $('#password').val()){
            passwordConfirmHelpBlock.css({color:'green',opacity:1,dispaly:''});
            passwordConfirmHelpBlock.html('密码一致');
        }else {
            passwordConfirmHelpBlock.css({color:'red',opacity:1,dispaly:''});
            passwordConfirmHelpBlock.html('密码不一致');
        }
    });
    $('#passwordConfirm').blur(function () {
        var passwordConfirmHelpBlock = $('#passwordConfirmHelpBlock');
        if($('#passwordConfirm').val() !== $('#password').val()){
            passwordConfirmHelpBlock.css({color:'red',opacity:1,dispaly:''});
            passwordConfirmHelpBlock.html('密码不一致');
        }
    });
    $('#passwordConfirm').focus(function () {
        var passwordConfirmHelpBlock = $('#passwordConfirmHelpBlock');
        console.info($('#passwordConfirm').val());
        if($('#passwordConfirm').val() !== $('#password').val()){
            passwordConfirmHelpBlock.css({color:'red',opacity:1,dispaly:'none'});
            passwordConfirmHelpBlock.html('密码不一致');
        }else if ($('#passwordConfirm').val() === $('#password').val()){
            passwordConfirmHelpBlock.css({color:'green',opacity:1,dispaly:''});
            passwordConfirmHelpBlock.html('密码一致');
        }
    });

    $('#registerBtn').click(function () {
        if(!(document.getElementsByName('geetest_challenge')[0].value && document.getElementsByName('geetest_validate')[0].value && document.getElementsByName('geetest_seccode')[0].value)){
            var registerHelpBlock = $('#registerHelpBlock');
            registerHelpBlock.css({'opacity':'1','display':''});
            for(var i = 0;i < 6; i++){
                $('#captcha-box').fadeToggle('fast');
            }

            setTimeout(function () {
                $('#registerHelpBlock').css({'display':'none'});
            },2000)
        }else if (!$('#username').val()) {
            alert('请输入用户名');
        }else if(!$('#password').val()) {
            alert('请输入密码');
        }else if($('#passwordConfirm').val() !== $('#password').val()){
            alert('密码不一致');
        }else {
            $('#data').submit();
        }
    });

    $.ajax({
        url:'ajaxGetVerify/t/'+(new Date()).getTime(),
        type:'GET',
        dataType:'json',
        success:function (data) {
            initGeetest({
                // 以下配置参数来自服务端 SDK
                gt: data.gt,
                challenge: data.challenge,
                offline: !data.success,
                new_captcha: data.new_captcha,

                width:'100%'
            }, function (captchaObj) {
                // 这里可以调用验证实例 captchaObj 的实例方法
                captchaObj.appendTo('#captcha-box');
                captchaObj.bindForm('#data');
                validate = captchaObj.getValidate();
            })
        }
    });

});
